package com.qfedu.openplateform.manager.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.RechargeService;
import com.qfedu.openplateform.manager.util.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/recharge")
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;

    @RequestMapping("/table")
    public TableData findAllRecharge (Integer page, Integer limit) {

        PageHelper.startPage(page,limit);

        List<Recharge> recharge = rechargeService.findAllRecharge(page, limit);

        PageInfo pageInfo = new PageInfo(recharge);

        TableData<Recharge> tableData = new TableData<>();

        tableData.setCount(pageInfo.getTotal());
        tableData.setData(recharge);

        return tableData;

    }

    @RequestMapping("/del")
    public AjaxMessage deleteRecharge (Integer[] ids) {

        try {
            rechargeService.deleteRecharge(ids);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(false, "删除失败");
        }

    }


    @RequestMapping("/add")
    public AjaxMessage insertRecharge (Recharge recharge) {

        Date date = new Date();

        recharge.setOrderId(Long.valueOf(KeyUtil.genUniqueKey()));
        recharge.setCreatetime(date);
        recharge.setUpdatetime(date);

        int i = rechargeService.insertRecharge(recharge);

        AjaxMessage ajaxMessage = new AjaxMessage();

        if (i > 0){

            ajaxMessage.setStatus(true);
            return ajaxMessage;

        } else {

            ajaxMessage.setStatus(false);
            ajaxMessage.setMessage("添加失败");
            return ajaxMessage;

        }
    }

    @RequestMapping("/update")
    public AjaxMessage updateRecharge (Recharge recharge) {

        recharge.setUpdatetime(new Date());

        int i = rechargeService.updateRecharge(recharge);

        AjaxMessage message = new AjaxMessage();

        if (i > 0) {

            message.setStatus(true);
            return message;

        } else {

            message.setStatus(false);
            message.setMessage("更新数据失败");
            return  message;

        }

    }



}
